function TraceComparison_GreenFastVsGreenSlow
%TRACECOMPARISON Summary of this function goes here
%   Detailed explanation goes here

ylim_1 = -200;
ylim_2 = 2500;

global expPath;
path = expPath;

%% Load mut2 colony
load([path 'annotation.mat']);

mut2 = 'colony-FOV004-003_conn_mut2.mat';
load([path mut2]);
colony.exp = annotation.exp;
colony.chip = annotation.chip;

tUnit = colony.exp.TimeInt;
pathCellIndx = GetRedundantPathsII(colony);
list = pathCellIndx(10).lista;
[f, df, time, cellDiv] = FormatPathData(colony, list);
time = tUnit*time;
cellDiv = tUnit*cellDiv;
color = [72+30 255 66+30]/255;
colorLine = ([72 244 66]-50)/255;
shade1_h = PlotTrace(time, df, cellDiv, color, colorLine);

ylim([ylim_1 ylim_2])

xlabel('\fontsize{17}Time (min)')

set(gca,'box','off')
isholdonque = ishold; 
hold on 
ax = axis; 
plot(ax(2)*[1,1],ax(3:4),'k','linewidth',0.5) 
plot(ax(1:2),ax(4)*[1,1],'k','linewidth',0.5) 
if isholdonque == 0 
hold off 
end


current = get(gca,'DataAspectRatio');
current(2) = 4*current(2);
set(gca, 'DataAspectRatio', current);


%% Load mut1 colony
load([path 'annotation.mat']);

mTurq = 'colony-FOV004-002_conn_mut1.mat';
load([path mTurq]);
colony.exp = annotation.exp;
colony.chip = annotation.chip;

tUnit = colony.exp.TimeInt;
pathCellIndx = GetRedundantPathsII(colony);
list = pathCellIndx(1).lista;
[f, df, time, cellDiv] = FormatPathData(colony, list);
time = tUnit*time;
cellDiv = tUnit*cellDiv;
color = ([16 147 11]+30)/255;
colorLine = ([16 147 11]-10)/255;
shade2_h = PlotTrace(time, df, cellDiv, color, colorLine);

str1 = {'Slow (t_{50}=14.9min), mEGFP'};
text(165, 2100, str1, 'FontSize', 18)
ylim([ylim_1 ylim_2])
set(gca, 'XTickLabel', []);

current = get(gca,'DataAspectRatio');
current(2) = 4*current(2);
set(gca, 'DataAspectRatio', current);

set(gca,'box','off')
isholdonque = ishold;
hold on 
ax = axis; 
plot(ax(2)*[1,1],ax(3:4),'k','linewidth',0.5) 
plot(ax(1:2),ax(4)*[1,1],'k','linewidth',0.5) 
if isholdonque == 0 
hold off 
end



%Show both traces in same axes
fig_h = figure('Position', [300, 100, 900, 600]);
ax = subplot(1,1,1);


figure(shade2_h)
h = get(gcf,'Children');
newh2 = copyobj(h, fig_h);
posnew = get(newh2,'Position');
posnew(2) = 0.25;
set(newh2, 'Position', posnew)
uistack(newh2, 'bottom')

figure(shade1_h)
h = get(gcf, 'Children');
newh1 = copyobj(h, fig_h);
posnew = get(newh1,'Position');
posnew(2) = -0.05;
set(newh1, 'Position', posnew)
uistack(newh1, 'bottom')


figure(fig_h)
str1 = {'\fontsize{17}Fluorescence Production Rate (au)'};%, 'Repressed \it\fontsize{22}plac  (au)'};
text(-0.1, 0.13, str1, 'Rotation', 90)

axis off
set(gcf, 'Color', 'w')

title_h = title('')

str1 = {'Fast (t_{50}=5.3min), mGFPmut2'};
text(0.275, 0.43, str1, 'FontSize', 18)


close(figure(1))
close(figure(2))


end

function shade_h = PlotTrace(time, df, cellDiv, color, colorLine)

    threshold  = 325;

    shade_h = figure;
    hold on;
    minmaxT = [time(1) time(end)];
    
    dfBW = df;
    dfBW(df>=threshold)=1;
    indx = df<threshold;
    dfBW(indx) = 0;
    
    stats = regionprops(logical(dfBW), 'BoundingBox');

    nPeak = length(stats);
    plot(time, df, 'linewidth', 1.5, 'color', colorLine);
%    hold on
    for j=1:nPeak
        ti = floor(stats(j).BoundingBox(1));
        tf = ti+ceil(stats(j).BoundingBox(3));
        if j==1, ti = ti+1; end
%         if j==nPeak, tf = tf-1; end

        %Calculate intersection with threshold

        m = (df(ti+1) - df(ti))/(time(ti+1) - time(ti));
        b = df(ti) - m*time(ti);
        time_i =  (threshold - b)/m;
        
        if tf==length(time), 
            time_ = [time_i time(ti+1:tf) ];
            df_ = [threshold df(ti+1:tf) ];
        else
            m = (df(tf+1) - df(tf))/(time(tf+1) - time(tf));
            b = df(tf) - m*time(tf);
            time_f =  (threshold - b)/m;
            time_ = [time_i time(ti+1:tf) time_f];
            df_ = [threshold df(ti+1:tf) threshold];
        end
        
        plot(time_, df_, 'linewidth', 2, 'color', colorLine);
%         plot(time(ti:tf), df(ti:tf), '-k');        
        
        area(time_, df_, threshold, 'FaceColor', color, 'LineStyle', 'none');
%        h = area(time(ti:tf), threshold*ones(1,length(ti:tf)), 'FaceColor', [30 255 13]/255);        

    end
        
    %Plot lines to indicate cell division
    for j=2:length(cellDiv)
        plot(cellDiv(j), -100, '.k', 'MarkerSize', 13)
%         line([-1.5 1.5]+cellDiv(j), -100*[1 1], 'Color', 'k', 'LineStyle','-', 'LineWidth', 2)
    end
    line([-100 2000], [1 1]*0, 'Color', 'k', 'LineStyle','-')
    line([-100 2000], [1 1]*threshold, 'Color', 'k', 'LineStyle','--', 'LineWidth', 2)

    xlim(minmaxT);
    xlim([0 583])
    ylim([-200 2000]);
    box on;
%    set([hxlabel hylabel], 'FontSize', 15);
    set(gca, 'FontSize', 13);
    
    hold off;

end

function [f, df, time, cellDiv] = FormatPathData(colony, list)

cell = colony.cell;

time = []; limite = [];
cellDiv = [];   
l = []; dl = [];
ls = []; dls = [];
f = []; df = [];
fs = []; dfs = [];

maxRows = 0;
nImages = 0;
for j=1:length(list)
    
    indx = list(j);
    
    rawLength = max(ceil(cell(indx).bottomLim) - floor(cell(indx).topLim));
    if maxRows<rawLength; maxRows = rawLength; end;
    nImages = nImages + length(cell(indx).frameID);
    
    cellDiv = [cellDiv cell(indx).frameID(1)];
    time = [time cell(indx).frameID];
    f = [f cell(indx).flscnc.f];
    df = [df cell(indx).flscnc.dfFilt];
    l = [l cell(indx).size.lFilt];
    dl = [dl cell(indx).size.dlFilt];
    limite = [limite time(end)];
    
    if j==1
        ls = l;
        dls = dl;
        fs = f;
        dfs = df;
    else
        %Encontrar a la madre de indx
        indxM = cell(indx).mother;
        %Encontrar a la hermana de indx
        indxS = cell(indxM).daughters(1);
        if indxS == indx, indxS = cell(indxM).daughters(2); end
        
        %Empezar el trace en donde empieza el trace de la hermana
        traceLength = length(cell(indx).flscnc.rawf);
        CurrentTraceLength = length(f);
        indxStartTraceS = CurrentTraceLength - traceLength+ 1;
        
        indxEndTraceS = indxStartTraceS + length(cell(indxS).flscnc.rawf)-1;
        
        ls(indxStartTraceS:indxEndTraceS) = cell(indxS).size.lFilt;
        dls(indxStartTraceS:indxEndTraceS) = cell(indxS).size.dlFilt;
        fs(indxStartTraceS:indxEndTraceS) = cell(indxS).flscnc.f;
        dfs(indxStartTraceS:indxEndTraceS) = cell(indxS).flscnc.df;
    end
end
f = f(1:end-1);
df = df(1:end-1);
time = time(1:end-1);

end
